﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Flower.Client;
using Flower.Services;
using Flower.Services.Data;
using NUnit.Framework;
using System.Threading;
using Flower.Testing;

namespace Flower.Directory.Host.Default.Tests
{
    [TestFixture]
    public class AdvicesTests
    {
        public class Processor : IProcessor
        {
            public static int CheckPendingProcessesCount;

            public void CheckPendingProcesses()
            {
                Interlocked.Increment(ref CheckPendingProcessesCount);
            }

            public void IsRunning(bool state)
            {
            }
        }

        [TestFixtureSetUp]
        public void Setup()
        {
            Processor.CheckPendingProcessesCount = 0;
        }

        [Test]
        public void ProcessorNotification()
        {
            var scriptEnginePool = new ScriptEnginePool(1);
            var proxyProvider = new DirectoryProxyProvider();
            var dir = new MemoryDirectory.Directory(scriptEnginePool, proxyProvider);
            
            string config = 
                @"<objects xmlns=""http://www.springframework.net"">" +
                @"  <object id=""" + Advices.ProcessorNotifier.ProcessorObjectName + @""" type=""" + typeof(Processor).FullName + @""" />" +
                @"</objects>";

            new DirectoryBuilder(dir)
                .Root("/Processors")
                    .Processor("TestProcessor1", config)
                        .Folder("Running").End()
                        .Folder("Pending").End()
                    .End()
                    .Processor("TestProcessor2", config)
                        .Folder("Running").End()
                        .Folder("Pending").End()
                    .End()
                    .Processor("TestProcessor3", (string)null)
                        .Folder("Running").End()
                        .Folder("Pending").End()
                    .End()
                .End();

            using (var trn = new TransactionScope())
            {
                IDirectory dirProxy = proxyProvider.MakeProxy(dir, DirectoryProxyProvider.Advices.ProcessorNotification);

                dirProxy.Save("/Processors/TestProcessor1/Pending", new Process());
                dirProxy.Save("/Processors/TestProcessor2/Pending", new Process());
                dirProxy.Save("/Processors/TestProcessor3/Pending", new Process());

                var pid = dirProxy.Save("/Processors/TestProcessor1/Running", new Process());
                dirProxy.Move(pid, "/Processors/TestProcessor1/Pending");

                new DirectoryBuilder(dir)
                    .Root("/Processors")
                        .Processor("TestProcessor4", config)
                            .Folder("Running").End()
                            .Folder("Pending").End()
                        .End()
                    .End();

                dirProxy.Save("/Processors/TestProcessor4/Pending", new Process());

                trn.Complete();
            }

            int limit = 10;

            do
            {
                Thread.Sleep(1000);
            }
            while (Processor.CheckPendingProcessesCount == 0 && limit-- > 0);

            Assert.IsTrue(Processor.CheckPendingProcessesCount > 0);
        }
    }
}
